//package wmmd.disc.webmaster;

import java.io.*;
//import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.DatabaseConnector;

public class ShowAllTopics extends HttpServlet {
    
    //----------------------------------------------------------------------------------------
    /**
     * Called by AllTopics.jsp - will display all unique topics in the specified table
     */
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        //The JSP document passes in which db table to use
        String tablename = request.getParameter("tName");
        
        int ds = 0;   //for parsing the date string
        int lRTT = 0;   //message's topic ID
        int CurrLevel = 0;  //current topic level
        int ELevels = 0;   //tracks number of list levels
        int nummess = 0;   //tracks number of messages
        
        //Variables for the database connection
        DatabaseConnector con1 = null;
        DatabaseConnector con2 = null;
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        try {
            con1 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
            con2 = new DatabaseConnector(DatabaseConnector.getDatabaseType());
            
            //Get the topic to be expanded -- 0 if none.
            if(request.getParameter("RTT")!=null) {
                lRTT = 0 + Integer.valueOf(request.getParameter("RTT")).intValue();
            }
            
            //Create recordset object and retrieve info from table
            //TODO: Query
            String SQL = "SELECT Topic,TLevel,Subject FROM " + tablename + " WHERE Topic > 0 AND ResponseID = Topic ORDER BY ResponseID";
            String[][] rs1 = con1.selectSQL(SQL,3);
            
            //Print out each topic in a <TABLE>
            out.println("<TABLE><TR><TD>Topic</TD><TD>Responses</TD><TD>Action</TD></TR>");

            for(int i=0;i<rs1.length;i++){
            //while(rs1.next()) {
                String topic_string = rs1[i][0];
                String tlevel_string = rs1[i][1];
                String subject_string = rs1[i][2];
                /*String topic_string = rs1.getString("Topic");
                String tlevel_string = rs1.getString("TLevel");
                String subject_string = rs1.getString("Subject");*/
                //TODO:
                SQL = "SELECT * FROM " + tablename + " WHERE Topic = ?";
                String[][] rs2 = con2.selectSQL(SQL,1,new Object[]{topic_string});

                rs2 = con2.selectWhereSQL(tablename, new String[]{"*"}, "Topic", topic_string);
                nummess = -1;
                
                //Keep track of the number of records
                for (int j=0;j<rs2.length;j++){
                    nummess = nummess + 1;
                }
                
                out.print("<TR><TD>" + topic_string + ". " + subject_string + "</TD>");
                out.print("<TD>" + nummess + "</TD>");
                out.println("<TD><A href =\"AllTopics.jsp?RTT=" + topic_string + "\">");
                out.println("Expand</A></LI>" + " </TD></TR>");
                
                //Find any subtopics within the current topic, and print them out
                if(lRTT == Integer.valueOf(topic_string).intValue()) {
                    SQL = "SELECT ID,TLevel,Name,Date,Subject,Message,ResponseID FROM " + tablename + " WHERE Topic = ? ORDER BY ResponseID";
                    rs2 = con2.selectSQL(SQL,7,new Object[]{topic_string});

                    out.print("<TR><TD COLSPAN = 3>");
                    out.print("<UL>");
                    CurrLevel = Integer.valueOf(tlevel_string).intValue();
                    ELevels = 0;

                    for (int j=0;j<rs2.length;j++){
                        String id2_string = rs2[i][0];
                        int CurrLevel2 = Integer.valueOf(rs2[i][1]).intValue();
                        String name2_string = rs2[i][2];
                        String date2_string = rs2[i][3];
                        String subject2_string = rs2[i][4];
                        String message2_string = rs2[i][5];
                        /*String id2_string = rs2.getString("ID");
                        int CurrLevel2 = Integer.valueOf(rs2.getString("TLevel")).intValue();
                        String name2_string = rs2.getString("Name");
                        String date2_string = rs2.getString("Date");
                        String subject2_string = rs2.getString("Subject");
                        String message2_string = rs2.getString("Message");*/
     
                        //if the message has the same topic level, print the message in a textarea
                        if(CurrLevel2 == CurrLevel) {
                            out.println("<FORM METHOD=\"POST\" ACTION=\"displaymessage.jsp\"><LI>");
                            out.print(name2_string + " posted " + subject2_string);
                            out.println(" on " + date2_string);
                            out.print("<TEXTAREA NAME=\"Message\" COLS=\"60\" ROWS=\"10\" WRAP>");
                            out.print(message2_string + "</TEXTAREA>");
                            out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"RTID\" VALUE=\"" + id2_string + "\">");
                            out.println("<INPUT TYPE=\"SUBMIT\" value=\"Respond\" >");
                            out.println("</LI></FORM>");
                        }
                        //if the message is a subtopic, indent, and then print the message in a textarea
                        else if(CurrLevel2 > CurrLevel) {
                            out.print("<UL>");
                            ELevels = ELevels + 1;
                            out.println("<FORM METHOD=\"POST\" ACTION=\"displaymessage.jsp\"><LI>");
                            out.print(name2_string + " posted " + subject2_string);
                            out.println(" on " + date2_string);
                            out.print("<TEXTAREA NAME=\"Message\" COLS=\"60\" ROWS=\"10\" WRAP>");
                            out.print(message2_string + "</TEXTAREA>");
                            out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"RTID\" VALUE=\"" + id2_string + "\">");
                            out.println("<INPUT TYPE=\"SUBMIT\" value=\"Respond\"  id=\"SUBMIT\"1 name=\"SUBMIT\"1>");
                            out.println("</LI></FORM>");
                            CurrLevel = CurrLevel2;
                        }
                        //Return the UL list to main level
                        else {
                            int jIndex = 0;
                            for(jIndex=(CurrLevel2 + 1); jIndex==CurrLevel; jIndex++) {
                                out.println("</UL>");
                                ELevels = ELevels - 1;
                            }
                            out.println("<FORM METHOD=\"POST\" ACTION=\"displaymessage.jsp\"><LI>");
                            out.print(name2_string + " posted " + subject2_string);
                            out.println(" on " + date2_string);
                            out.print("<TEXTAREA NAME=\"Message\" COLS=\"60\" ROWS=\"10\" WRAP>");
                            out.print(message2_string + "</TEXTAREA>");
                            out.println("<INPUT TYPE=\"HIDDEN\" NAME=\"RTID\" VALUE=\"" + id2_string + "\">");
                            out.println("<INPUT TYPE=\"SUBMIT\" value=\"Respond\"  id=\"SUBMIT\"1 name=\"SUBMIT\"1>");
                            out.println("</LI></FORM>");
                        }
                    }
                }
                //Return the UL list to the main level
                while(ELevels > 0) {
                    out.print("</UL>");
                    ELevels = ELevels - 1;
                }
                out.println("</UL><TD></TR>");
            }
            
            //Clean up
            out.print("</TABLE>");
        }
        finally
        {
            con1.CleanUp();
            con2.CleanUp();
        }
    }
}

